import { db } from "./config";

let _ = require("lodash");
let fs = require("fs");
// 数据库插入
export let guzhuang_insert: (dbs: String, pam: any[]) => {} = async (
  dbs: String,
  pam: any[]
) => {
  // INSERT INTO table(a, b, c) VALUES(1, 2, 3)
  // ON DUPLICATE KEY UPDATE c = c + 1;
  // let insertSql = `INSERT INTO ${dbs}(title,title_id,name,season,url,type) VALUES(?,?,?,?,?,?)`;
  let insertSql = `INSERT INTO ${dbs}(title,title_id,name,season,url,type,yk_vd_id)  SELECT ${pam.join(
    ","
  )} FROM DUAL  WHERE NOT EXISTS(SELECT ${pam[4]} FROM gu_zhuang WHERE url = ${
    pam[4]
    })`;

  let res = await db(insertSql, pam);
  return res;
};



//目录插入
export let contents_insert = async (title: any[], updateValue: any[], videoLink: any[]) => {

  let insertSql = `INSERT INTO contents(${title.join(
    ","
  )}) SELECT ${updateValue.join(
    ","
  )} FROM DUAL WHERE NOT EXISTS(SELECT video_link FROM contents WHERE  video_link =  ${videoLink.join(
    ","
  )}) `;

  // fs.writeFileSync("./sqyl语句.js", insertSql, "utf-8");
  await db(insertSql);

};

//目录分页查询 
export let contents_query = async ({ searchValue, start, end }) => {
  // console.count("目录分页查询" + searchValue + '------' + start + '-------' + end)

  let sql = `SELECT * FROM contents WHERE category = '${searchValue}' ORDER BY id  LIMIT ${start},${end}`;
  let res = await db(sql);

  // 转换object  kye 值下划线大小写
  res = _.map(res, item => {


    //驼峰转换
    // let obj = {}
    // _.forEach(item, (value, key) => {
    //   obj[_.camelCase(key)] = value;

    // })
    // console.log(res)
    return res;
  })

  return res;
}

// 查询满足条件的 条数
export let count_equal = async (searchValue) => {
  // console.log("条数查询" + searchValue)

  let sql = `SELECT COUNT(id) FROM contents WHERE category = '${searchValue}' ORDER BY id`;
  let res = await db(sql);
  return JSON.stringify(res)

}

// 格剧集分类插入
// 数据库插入
export let tv_series_insert: (dbs: String, pam: any[]) => {} = async (
  dbs: String,
  pam: any[]
) => {
  // console.count("剧集类插入" + dbs)

  let insertSql = `INSERT INTO ${dbs}(title,title_id,name,season,url,category,yk_video_id)  SELECT ${pam.join(
    ","
  )} FROM DUAL  WHERE NOT EXISTS(SELECT yk_video_id FROM ${dbs} WHERE yk_video_id = ${
    pam[6]
    })`;
  let res = await db(insertSql);


  return res;
};
export let movie_insert = async (
  dbs: String, table: any[], value: any[], searchValue: String
) => {

  let insertSql = `INSERT INTO ${dbs}(${table.join(',')})  SELECT ${value.join(
    ","
  )} FROM DUAL  WHERE NOT EXISTS(SELECT video_id FROM ${dbs} WHERE video_id = '${
    searchValue
    }')`;
  // console.log(insertSql)
  let res = await db(insertSql);


  return res;
};